﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace AppToolkit.WPF.UI
{
    /// <summary>
    /// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
    ///
    /// Step 1a) Using this custom control in a XAML file that exists in the current project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:WPFExtensions.Controls"
    ///
    ///
    /// Step 1b) Using this custom control in a XAML file that exists in a different project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:WPFExtensions.Controls;assembly=WPFExtensions.Controls"
    ///
    /// You will also need to add a project reference from the project where the XAML file lives
    /// to this project and Rebuild to avoid compilation errors:
    ///
    ///     Right click on the target project in the Solution Explorer and
    ///     "Add Reference"->"Projects"->[Browse to and select this project]
    ///
    ///
    /// Step 2)
    /// Go ahead and use your control in the XAML file.
    ///
    ///     &lt;MyNamespace:FolderBrowser/&gt;
    ///
    /// </summary>
    public class FolderBrowser : TreeView
    {
        private readonly object _dummyNode = null;

        static FolderBrowser()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(FolderBrowser), 
                new FrameworkPropertyMetadata(typeof(FolderBrowser)));
        }

        /// <summary>
        /// ctor
        /// </summary>
        public FolderBrowser()
        {
            this.Loaded += new RoutedEventHandler(OnLoaded);
        }

        void OnLoaded(object sender, RoutedEventArgs e)
        {
            parseDrives();
        }

        private void parseDrives()
        {
            foreach (string drive in Directory.GetLogicalDrives())
            {
                TreeViewItem item = new TreeViewItem();
                item.Header = drive;
                item.Tag = drive;
                item.Items.Add(_dummyNode);
                item.Expanded += folder_Expanded;

                // Apply the attached property so that 
                // the triggers know that this is root item.
                TreeViewItemProps.SetIsRootLevel(item, true);

                this.Items.Add(item);
            }
            this.UpdateLayout();
        }

        private void folder_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = (TreeViewItem)sender;
            if (item.Items.Count == 1 && item.Items[0] == _dummyNode)
            {
                item.Items.Clear();
                try
                {
                    foreach (string dir in Directory.GetDirectories(item.Tag as string))
                    {
                        TreeViewItem subitem = new TreeViewItem();
                        subitem.Header = new DirectoryInfo(dir).Name;
                        subitem.Tag = dir;
                        subitem.Items.Add(_dummyNode);
                        subitem.Expanded += folder_Expanded;
                        item.Items.Add(subitem);
                    }
                }
                catch (Exception) { }
            }
        }

        #region Attached TreeViewItemProps
        internal static class TreeViewItemProps
        {
            public static bool GetIsRootLevel(DependencyObject obj)
            {
                return (bool)obj.GetValue(IsRootLevelProperty);
            }

            public static void SetIsRootLevel(
                DependencyObject obj, bool value)
            {
                obj.SetValue(IsRootLevelProperty, value);
            }

            public static readonly DependencyProperty IsRootLevelProperty =
                DependencyProperty.RegisterAttached(
                "IsRootLevel",
                typeof(bool),
                typeof(TreeViewItemProps),
                new UIPropertyMetadata(false));
        } 
        #endregion
    }
}
